import React, { Component } from 'react'
import styles from './index.module.scss'
import { Link } from 'react-router-dom'
import {
  Card,
  Breadcrumb,
  Form,
  Button,
  Radio,
  DatePicker,
  Table,
  Tag,
  Space,
  Modal,
  message
} from 'antd'
import {
  EditOutlined,
  DeleteOutlined,
  ExclamationCircleOutlined
} from '@ant-design/icons'
import { ArticleStatus } from 'api/constants'

import { getArticles, delArticles } from 'api/acticle'

import defaultImg from 'assets/error.png'

import Channel from 'components/Channel'
const { confirm } = Modal

export default class ArtcleList extends Component {
  columns = [
    {
      title: '封面',
      render: obj => {
        if (obj.cover.type === 0)
          return (
            <img
              src={defaultImg}
              alt=""
              style={{ width: 200, height: 120, objectFit: 'cover' }}
            />
          )
        else {
          return (
            <img
              src={obj.cover.images[0]}
              alt=""
              style={{ width: 200, height: 120, objectFit: 'cover' }}
            />
          )
        }
      }
    },
    {
      title: '标题',
      dataIndex: 'title'
    },
    {
      title: '状态',
      dataIndex: 'status',
      render: status => {
        const obj = ArticleStatus.find(item => item.id === status)
        return (
          <Tag color={obj.color} style={{ width: 70, textAlign: 'center' }}>
            {obj.name}
          </Tag>
        )
      }
    },
    {
      title: '发布时间',
      dataIndex: 'pubdate'
    },
    {
      title: '阅读数',
      dataIndex: 'read_count'
    },
    {
      title: '评论数',
      dataIndex: 'comment_count'
    },
    {
      title: '点赞数',
      dataIndex: 'like_count'
    },
    {
      title: '操作',
      render: data => {
        return (
          <Space>
            <Button
              type="primary"
              shape="circle"
              icon={<EditOutlined />}
              onClick={() => this.handleEdit(data.id)}
            />
            <Button
              type="primary"
              danger
              shape="circle"
              icon={<DeleteOutlined />}
              onClick={() => this.delClick(data.id)}
            />
          </Space>
        )
      }
    }
  ]
  // 存放请求参数
  reqParams = {
    page: 1,
    per_page: 10
  }

  state = {
    articles: []
  }
  render() {
    const { results, total_count, page, per_page } = this.state.articles
    return (
      <div className={styles.root}>
        <Card
          title={
            <Breadcrumb>
              <Breadcrumb.Item>
                <Link to="/home">首页</Link>
              </Breadcrumb.Item>
              <Breadcrumb.Item>文章列表</Breadcrumb.Item>
            </Breadcrumb>
          }
        >
          {/* 表单 
          initialValues: 默认值  onFinish提交事件 获取formitem绑定对应name表单的值
          */}
          <Form initialValues={{ status: -1 }} onFinish={this.onFinish}>
            {/* 状态 */}
            <Form.Item label="状态" name="status">
              <Radio.Group>
                {ArticleStatus.map(item => (
                  <Radio value={item.id} key={item.id}>
                    {item.name}
                  </Radio>
                ))}
              </Radio.Group>
            </Form.Item>

            {/* 频道 */}
            <Form.Item label="频道" name="channel_id">
              <Channel></Channel>
            </Form.Item>
            {/* 日期*/}
            <Form.Item label="日期" name="date">
              <DatePicker.RangePicker />
            </Form.Item>
            <Form.Item>
              <Button type="primary" htmlType="submit">
                筛选
              </Button>
            </Form.Item>
          </Form>
        </Card>
        {/* 表格 */}
        <Card title={`根据筛选条件共查询到 ${total_count} 条结果：`}>
          <Table
            rowKey={record => record.id}
            columns={this.columns}
            dataSource={results}
            pagination={{
              total: total_count,
              position: ['bottomCenter'],
              current: page,
              pageSize: per_page,
              onChange: this.onChange
            }}
          />
        </Card>
      </div>
    )
  }
  componentDidMount() {
    this.getArticles()
  }
  // 点击筛选
  onFinish = ({ status, channel_id, date }) => {
    // this.reqParams = {
    //   page: 1,
    //   status: status !== -1 ? status : null,
    //   channel_id: channel_id || null,
    //   begin_pubdate: date
    //     ? date[0].startOf('day').format('YYYY-MM-DD HH-mm-ss')
    //     : null,
    //   end_pubdate: date
    //     ? date[1].endOf('day').format('YYYY-MM-DD HH-mm-ss')
    //     : null
    // }
    // console.log(this.reqParams)
    // this.getArticles()

    if (status !== -1) {
      this.reqParams.status = status
    } else {
      delete this.reqParams.status
    }
    if (channel_id !== undefined) {
      this.reqParams.channel_id = channel_id
    } else {
      delete this.reqParams.channel_id
    }
    if (date) {
      this.reqParams.begin_pubdate = date[0].format('YYYY-MM-DD')
      this.reqParams.end_pubdate = date[1].format('YYYY-MM-DD')
    } else {
      delete this.reqParams.begin_pubdate
      delete this.reqParams.end_pubdate
    }
    this.reqParams.page = 1
    console.log(this.reqParams)
    this.getArticles()
  }

  // 文章列表
  getArticles = async () => {
    // console.log(this.reqParams)
    const res = await getArticles(this.reqParams)
    console.log(res)
    this.setState({
      articles: res.data
    })
  }
  // 分页改变
  onChange = (page, pageSize) => {
    this.reqParams.page = page
    this.reqParams.per_page = pageSize
    this.getArticles()
  }
  // 删除
  delClick = id => {
    confirm({
      title: '温馨提示',
      icon: <ExclamationCircleOutlined />,
      content: '你确定要删除这篇文章?',
      onOk: async () => {
        console.log('OK')
        await delArticles(id)
        this.getArticles()
        message.success('删除成功')
      }
    })
  }
  // 修改
  handleEdit = id => {
    this.props.history.push('/home/publish/' + id)
  }
}
